// rect-behavior.js
let systemInfo = {}
const app = getApp()
module.exports = Behavior({
  data: {
    mainHeight: 0,
    statusBarHeight: 44,
  },
  created(){
    wx.getSystemInfo({
      success: res => systemInfo = res,
      fail: err => console.log(err)
    })
  },
  methods: {
    /**
     * 获取选择器的高度，两个参数：1、选择器数组 2、是否使用原生底部导航，计算滚动的高度3、回调函数
     */
    getRect(){
      let seletor = [...arguments]
      console.log(seletor);
      let isOriginBottomBar = seletor[1] ? seletor[1] : false
      var that = this;

      systemInfo = app.globalData.systemInfo
      console.log(systemInfo);

  　　//我这里需要获取多个元素的高度，所以用的是selectAll
      wx.createSelectorQuery().selectAll(seletor[0].join()).boundingClientRect().exec(function(res){
        console.log(res);
        let allRectH = []
        const { screenHeight, windowHeight, safeArea, statusBarHeight } = systemInfo
        console.log(screenHeight, windowHeight, safeArea, statusBarHeight);

        for (let i = 0; i < seletor[0]?.length; i++) {
          allRectH.push(res[0][i]?.height || 0)
        }
        const totalH = allRectH.reduce((prev,curr) => {
          return prev + (curr)
        }, 0)

        // 安全区域底部高度
        const safeAreaBottom = isOriginBottomBar ? 0 : screenHeight - safeArea.bottom
        console.log(safeAreaBottom);
        // 是否使用原生的底部导航
        const finallHeight = isOriginBottomBar == true ? windowHeight : screenHeight
        const mainHeight = finallHeight - totalH - safeAreaBottom
        that.setData({
          mainHeight, // 页面滚动区域的高度
          statusBarHeight, // 状态栏的高度
        })
        
        seletor[2] && typeof seletor[2] === 'function' && seletor[2]()
      })
    },
  }
})